package avicit.bdp.dcs.plugin.hdfs;

import avicit.bdp.common.utils.ConfigUtils;
import avicit.bdp.core.constant.Constants;
import avicit.bdp.dcs.plugin.BaseDataxParam;
import avicit.bdp.dcs.plugin.DataxConstants;
import avicit.bdp.dcs.plugin.DataxJsonInterface;
import avicit.bdp.dcs.plugin.hdfs.param.HdfsDataxParam;
import avicit.bdp.dcs.utils.JdbcConstants;

import java.util.HashMap;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间： 2020-08-01 11:39
 * @类说明：
 * @修改记录：
 */
public class HdfsDataxJson implements DataxJsonInterface {

    /**
     * 获取Reader插件名称
     * @return
     */
    @Override
    public String getReaderName() {
        return DataxConstants.HDFS_READER;
    }

    /**
     * 获取Writer插件名称
     * @return
     */
    @Override
    public String getWriterName() {
        return DataxConstants.HDFS_WRITER;
    }

    @Override
    public Map<String, Object> buildReader(BaseDataxParam param) {
        HdfsDataxParam dataxParam = (HdfsDataxParam) param;

        // 构建
        Map<String, Object> readerObj = new HashMap<>(4);
        readerObj.put(DataxConstants.NAME, getReaderName());
        Map<String, Object> parameterObj = new HashMap<>(16);
        parameterObj.put(DataxConstants.HDFS_PATH, dataxParam.getReaderPath());
        parameterObj.put(DataxConstants.HDFS_DEFAULTFS, dataxParam.getDefaultFS());
        parameterObj.put(DataxConstants.HDFS_FILE_TYPE, dataxParam.getFileType());
        parameterObj.put(DataxConstants.HDFS_FIELD_DELIMITER, dataxParam.getFieldDelimiter());
        parameterObj.put(DataxConstants.HDFS_COLUMN, dataxParam.getReaderColumn());
        parameterObj.put(DataxConstants.HDFS_ENCODING, DataxConstants.HDFS_UTF8);
        parameterObj.put(DataxConstants.HDFS_SKIP_HEADER, dataxParam.getSkipHeader());
        parameterObj.put(DataxConstants.HDFS_SKIP_LINES, dataxParam.getSkipLines());
        parameterObj.put(DataxConstants.HDFS_SHEET_TAB, dataxParam.getSheetTab());
        parameterObj.put(DataxConstants.HDFS_JSON_PATH, dataxParam.getJsonPath());
        parameterObj.put(DataxConstants.HDFS_QUOTATION, dataxParam.getQuotation());
        parameterObj.put(DataxConstants.HDFS_SINGLE_QUOTATION, dataxParam.getSingleQuotation());
        parameterObj.put(DataxConstants.HDFS_DOUBLE_QUOTATION, dataxParam.getDoubleQuotation());

        parameterObj.put(DataxConstants.HDFS_HAVE_KERBEROS, dataxParam.getHaveKerberos());
//        parameterObj.put(DataxConstants.KERBEROS_PARAM, dataxParam.getKerberosParam());

        readerObj.put(DataxConstants.PARAMETER, parameterObj);
        return readerObj;
    }

    @Override
    public Map<String, Object> buildWriter(BaseDataxParam param) {
        HdfsDataxParam dataxParam = (HdfsDataxParam) param;

        Map<String, Object> writerObj = new HashMap<>(4);
        writerObj.put(DataxConstants.NAME, getWriterName());
        Map<String, Object> parameterObj = new HashMap<>(16);

        parameterObj.put(DataxConstants.HDFS_HAVE_KERBEROS, dataxParam.getHaveKerberos());
        parameterObj.put(DataxConstants.HDFS_DEFAULTFS, dataxParam.getDefaultFS());
        parameterObj.put(DataxConstants.HDFS_FILE_TYPE, dataxParam.getFileType());
        parameterObj.put(DataxConstants.HDFS_WRITE_MODE, dataxParam.getWriteMode());
        parameterObj.put(DataxConstants.HDFS_FIELD_DELIMITER, dataxParam.getFieldDelimiter());
        parameterObj.put(DataxConstants.HDFS_HIVE_CONNECTION, dataxParam.getHiveConnection());
//        parameterObj.put(DataxConstants.KERBEROS_PARAM, dataxParam.getKerberosParam());

        if(JdbcConstants.HDFS.equals(dataxParam.getSourceType())){
            parameterObj.put(DataxConstants.HDFS_PATH, dataxParam.getWriterPath());
            parameterObj.put(DataxConstants.HDFS_FILENAME, dataxParam.getFileName());
            parameterObj.put(DataxConstants.HDFS_COLUMN, dataxParam.getWriterColumn());
        }else if(JdbcConstants.KAFKA.equals(dataxParam.getSourceType())){
            parameterObj.put(DataxConstants.HDFS_PATHS, dataxParam.getWriterPaths());
            parameterObj.put(DataxConstants.HDFS_MAX_FILESIZE, dataxParam.getMaxFileSize());
            parameterObj.put(DataxConstants.HDFS_TBL2_DEVICE_MAP, dataxParam.getTbl2DeviceMap());
            parameterObj.put(DataxConstants.HDFS_COLUMNS, dataxParam.getWriterColumns());
            parameterObj.put(DataxConstants.HDFS_TASK_TYPE, dataxParam.getTaskType());
        }

        writerObj.put(DataxConstants.PARAMETER, parameterObj);
        return writerObj;
    }

}
